<?php
    class Admin_CategoryController extends Main_Controller_Action
    {
        private $_arrParam;
        private $_category;
        
        public function init(){
            $this->loadTemplates('admin');
            $this->_arrParam = $this->_request->getParams();
            $this->_category = new Admin_Model_Category();
        }
        
        public function preDispatch(){
        	$auth = Zend_Auth::getInstance();
        	if(!$auth->hasIdentity()){
        		if($this->_request->getActionName() != 'login'){
        			$this->_redirect('admin/index/login');
        		}
        	}
        }
        
        public function indexAction(){
            $this->view->headTitle('Quản lý danh mục');
            
            if($_POST['add']){
                $this->_redirect('admin/category/create');
            }
            if($_POST['refresh']){
				$this->_redirect('admin/category');
			}
            if($_POST['delete']){
                $listId = $this->_arrParam['check'];
				$this->_category->deletecategorys($listId);
				$this->_redirect($_SERVER['HTTP_REFERER']);
            }
            
            $list = $this->_category->listcategory();
            $adapter = new Zend_Paginator_Adapter_DbSelect($list);
            $paginator = new Zend_Paginator($adapter);
            $paginator->setItemCountPerPage(40);
            $paginator->setPageRange(5);
            $currentPage = $this->_request->getParam('page', 1);
            $paginator->setCurrentPageNumber($currentPage);
            $this->view->data = $paginator;
            $this->view->total = $paginator->getTotalItemCount();
        }
        
        public function createAction(){
            $this->view->headTitle('Thêm loại');
            $filter = new Zend_Filter_StringTrim();
            $cat_name = $filter->filter($this->_arrParam['cat_name']);
            
            
            
            $validate = new Admin_Form_ValidateCategory();
            if($this->_request->isPost()){
                if($validate->validateCategoryName($cat_name)){
                    if($this->_category->checkcategory($cat_name) > 0){
                        $this->view->error = 'Tên loại đã tồn tại';
                    }
                    else{
                      
                        $flex = new Zendvn_Filter_RemoveCircumflex();
                        $lower  = new Zend_Filter_StringToLower();
                        $status = $this->_arrParam['status'];
                        $this->view->categoryNameValue = $cat_name;
                        $this->view->status = $status;
                        
                        
                        $data = array(
                            'cat_name' => $cat_name,
                            'status' => $status,
                        );
                        $result = $this->_category->addcategory($data);
                        
                        $data['cat_id'] = $result;
                        
                        $data['cat_name_filter'] = $lower->filter($flex->filter($cat_name));
                        $this->_category->saveAddItem($data, array('task' => 'add'));
                        $this->_redirect('admin/category');
                    }
                }
                else{
                    $this->view->error = 'Tên loại không được để trống và phải là chữ số';
                }
            }
        }
        
        public function editAction(){
            $this->view->headTitle('Sửa loại');
            $cat_id = $this->_arrParam['cat_id'];
            $this->view->data = $this->_category->getcategoryById($cat_id);
            
            if($this->_request->isPost()){
                $filter = new Zend_Filter_StringTrim();
                $cat_name = $filter->filter($this->_arrParam['cat_name']);
                $status = $this->_arrParam['status'];
                $validation = new Admin_Form_Validatecategory();
                $flag = 0;
                
                if($validation->validatecategoryName($cat_name)){
                    $this->view->categoryNameValue = $cat_name;
                }
                else{
                    $this->view->error = 'Tên loại không được để trống và phải là chữ số';
                    $flag++;
                }
                if($this->view->data['cat_name'] != $cat_name){
                    if($this->_category->checkcategory($cat_name, $cat_id) > 0){
                        $this->view->error = 'Tên loại đã tồn tại';
                        $flag++;
                    }
                }
                
                if($flag == 0){
                    $flex = new Zendvn_Filter_RemoveCircumflex();
                    $lower = new Zend_Filter_StringToLower();
                    $data = array(
                        'cat_name' => $cat_name,
                        'status' => $status,
                    );
                    $this->_category->editcategory($data, $cat_id);
                    $data['cat_id'] = $cat_id;
                    $data['cat_name_filter'] = $lower->filter($flex->filter($cat_name));
                    $this->_category->saveAddItem($data, array('task' => 'edit'));
                    $this->_redirect('admin/category');
                }
            }
        }
        
        public function deleteAction(){
            $this->_category->deletecategory($this->_arrParam['cat_id']);
            $this->_redirect('admin/category');
        }
        
        public function searchAction()
		{
			$this->view->headTitle('Tìm kiếm loại');
            $filter = new Zend_Filter_StringTrim();
            $flex   = new Zendvn_Filter_RemoveCircumflex();
            $lower  = new Zend_Filter_StringToLower();
            $flag   = 0;
            
			$keyword = $filter->filter($this->_arrParam['keyword']);
			$this->view->searchValue = $keyword;
			
			if($_POST['add']){
                $this->_redirect('admin/category/create');
            }
            if($_POST['refresh']){
				$this->_redirect('admin/category');
			}
            if($_POST['delete']){
                $listId = $this->_arrParam['check'];
				$this->_category->deletecategorys($listId);
				$this->_redirect($_SERVER['HTTP_REFERER']);
            }
			
			$validation = new Admin_Form_Validatecategory();
			if($validation->validateSearch($keyword))
			{
				$this->view->searchValue = $keyword;
			}
			else
			{
				$this->view->errorSearch = 'Bạn chưa nhập từ khóa';
                $keyword = $lower->filter($flex->filter($keyword));
                $keyword = 'cat_name_filter:' . $keyword;
				$result = $this->_category->searchcategory($keyword);
				$paginator = Zend_Paginator::factory($result);
				$paginator->setItemCountPerPage(40);
				$paginator->setPageRange(5);
				$currentPage = $this->_request->getParam('page', 1);
				$paginator->setCurrentPageNumber($currentPage);
					
				$this->view->total = count($result);
				$this->view->data = $paginator;
				$flag++;
			}
			
			if($flag == 0)
			{
                $keyword = $lower->filter($flex->filter($keyword));
                $keyword = 'cat_name_filter:' . $keyword;
				$result = $this->_category->searchcategory($keyword);
				$paginator = Zend_Paginator::factory($result);
				$paginator->setItemCountPerPage(40);
				$paginator->setPageRange(5);
				$currentPage = $this->_request->getParam('page', 1);
				$paginator->setCurrentPageNumber($currentPage);
				
				$this->view->total = count($result);
				$this->view->data = $paginator;
			}			
		}
    }
